package com.travelSpring.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.travelSpring.exception.UserException;
import com.travelSpring.mapper.ScenicCategoryMapper;
import com.travelSpring.pojo.entity.ScenicCategory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 功能：
 * 作者：卷心菜
 * 日期：2025/4/14 21:01
 */
@Service
public class ScenicCategoryService {

    @Resource
    ScenicCategoryMapper scenicCategoryMapper;

    public void save(ScenicCategory scenicCategory) {
        scenicCategoryMapper.insert(scenicCategory);
    }

    public void update(ScenicCategory scenicCategory) {
        scenicCategoryMapper.updateById(scenicCategory);
    }

    public List<ScenicCategory> query(ScenicCategory scenicCategory) {
        if (scenicCategory == null) {
            return Collections.emptyList(); // 或者返回new ArrayList<>()
        }
        LambdaQueryWrapper<ScenicCategory> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(scenicCategory.getName())) {
            lqw.like(ScenicCategory::getName, scenicCategory.getName());
        }
        lqw.orderByDesc(ScenicCategory::getId);
        return scenicCategoryMapper.selectList(lqw);
    }

    public void deleteBatch(List<ScenicCategory> list) {
        List<Integer> ids = list.stream()
                .filter(Objects::nonNull)
                .map(ScenicCategory::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        if(ids.isEmpty()) {
            throw new UserException("未提供有效商品ID");
        }
        scenicCategoryMapper.deleteBatchIds(ids);
    }

    public void delete(Integer id) {
        scenicCategoryMapper.deleteById(id);
    }
}
